//
//  JKPayCardVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/24.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKPayCardVC.h"

// vc
#import "ByUIScanCodeVC.h"

// other
#import "MKQRCode.h"
#import "FTPopOverMenu.h"
#import "ZKBombView.h"
#import "ZKShareView.h"

// model
#import "ByModelCardAndPay.h"

// api
#import "ByApiCardAndPay.h"

@interface JKPayCardVC ()

@property (nonatomic, strong) ByModelCardAndPay *model;
@end

@implementation JKPayCardVC
{
    UIImageView *_iconView;
    UILabel     *_nickLabel;
    UILabel     *_addressLabel;
    UILabel     *_cardLabel;
    
    UIImageView *_QRCodeView;
    
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的推荐码";
    self.view.backgroundColor = TABLE_COLOR;
    [[self.view viewWithTag:888] removeFromSuperview];
        
    [self addBYItemviews:@[@"推荐码-扫描", @"推荐码-分享"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    
    [self createViews];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
    
    [self fetchData];
}

#pragma mark - 请求
// 获取邀请码
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    ByModelUser *user = [ByCacheUser getUserInfo];
    model.code_type = user.account_id;
    model.account_id = user.uid;
    if (user.invite_code.length != 0) {
        self.model = [[ByModelCardAndPay alloc] init];
        self.model.invite_code = user.invite_code;
        self.model.url = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,user.invite_code,BY_USER.uid];
        
        [self updateUI];
    }
    else{
        [ByApiCardAndPay getInviteCode:model success:^(ByNetBaseResult *result) {
            if ([result.data isKindOfClass:[NSDictionary class]]) {
                self.model = [[ByModelCardAndPay alloc] initContentWithDic:result.data];
                self.model.url = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,self.model.invite_code,BY_USER.uid];
                user.invite_code = self.model.invite_code;
                user.url = self.model.url;
                [ByCacheUser setUserInfo:user];
                
                
                [self updateUI];
            }
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showOnlyText:result.msg];
        }];
        
    }
}

#pragma mark - 创建视图
- (void)createViews {
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.borderWidth = 1;
    bgView.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1].CGColor;
    [self.view addSubview:bgView];
    
    _iconView = [UIImageView new];
    _iconView.image = DEFAULT_IMAGE;
    _iconView.layer.cornerRadius = 20;
    _iconView.layer.masksToBounds = true;
    [bgView addSubview:_iconView];
    
    _nickLabel = [[UILabel alloc] init];
    _nickLabel.text = @"昵称";
    _nickLabel.font = FONT(16);
    _nickLabel.textAlignment = NSTextAlignmentLeft;
    _nickLabel.textColor = [UIColor blackColor];
    [bgView addSubview:_nickLabel];
    
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.text = @"地址";
    _addressLabel.font = FONT(14);
    _addressLabel.textAlignment = NSTextAlignmentLeft;
    _addressLabel.textColor = [UIColor lightGrayColor];
    [bgView addSubview:_addressLabel];
    
    _cardLabel = [[UILabel alloc] init];
    _cardLabel.text = @"推荐码";
    _cardLabel.font = FONT(14);
    _cardLabel.textAlignment = NSTextAlignmentRight;
    _cardLabel.textColor = [UIColor blackColor];
    [bgView addSubview:_cardLabel];
    
    UIView *sepLine = [UIView new];
    sepLine.backgroundColor = ZJ_SEPLINE_COLOR;
    [bgView addSubview:sepLine];
    
    _QRCodeView = [UIImageView new];
    _QRCodeView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [bgView addSubview:_QRCodeView];
    
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.text = @"用微信扫描二维码";
    noteLabel.font = FONT(14);
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.textColor = [UIColor lightGrayColor];
    [bgView addSubview:noteLabel];
    
    UIButton *copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    copyButton.backgroundColor = rgba(118, 197, 250, 1);
    copyButton.titleLabel.font = FONT(16);
    copyButton.layer.cornerRadius = 8;
    copyButton.layer.masksToBounds = true;
    copyButton.titleLabel.numberOfLines = 0;
    copyButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [copyButton setTitle:@"点击复制二维码链接\n(分享给好友)" forState:UIControlStateNormal];
    [copyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [copyButton addTarget:self action:@selector(copyQRCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:copyButton];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(@20);
        make.width.height.equalTo(@40);
    }];
    [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(20);
        make.bottom.equalTo(_iconView.mas_centerY).offset(-2);
    }];
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickLabel);
        make.top.equalTo(_iconView.mas_centerY).offset(2);
    }];
    [_cardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.centerY.equalTo(_addressLabel);
        make.left.greaterThanOrEqualTo(_addressLabel.mas_right).offset(10);
    }];
    [sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(_iconView.mas_bottom).offset(10);
        make.height.equalTo(@1);
    }];
    [_QRCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@140);
        make.top.equalTo(sepLine.mas_bottom).offset(30);
        make.centerX.equalTo(@0);
    }];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(_QRCodeView.mas_bottom).offset(10);
        make.height.equalTo(@20);
        
        make.bottom.equalTo(@-40);
    }];
    [copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.width.equalTo(@200);
        make.centerX.equalTo(@0);
        make.top.equalTo(bgView.mas_bottom).offset(10);
    }];
    
}

- (void)updateUI {
    
    ByModelUser *user = BY_USER;
    
    [_iconView setImageWithURL:[NSURL URLWithString:user.head] placeholderImage:DEFAULT_IMAGE completed:nil];
    _nickLabel.text = user.nickname;
    _addressLabel.text = @"";
    if (_model.invite_code.length != 0) {
        _cardLabel.text = [NSString stringWithFormat:@"推荐码：%@",_model.invite_code];
    }
    MKQRCode *code = [[MKQRCode alloc] init];
    if (self.model.invite_code.length != 0) {
        NSString *codeStr = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,self.model.invite_code,BY_USER.uid];
        [code setInfo:codeStr withSize:(SCREEN_WIDTH - 35*2) - 2*20];
        _QRCodeView.image = [code generateImage];
    }
    
}


#pragma mark - 事件
- (void)rightItemsAction:(NSInteger)rightIndex {
    
    // 扫描
    if (rightIndex == 0) {
        ByUIScanCodeVC *vc = [[ByUIScanCodeVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    // 分享
    else {
        ZKBombView *moreview = [[ZKBombView alloc] init];
        moreview.positionMode = ZKBombPositionModeBottom;
        moreview.damping = 0.8;
        // 创建内容视图
        ZKShareView *contentview = [[ZKShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        moreview.contentView = contentview;
        // 分享链接
        contentview.invite_code = self.model.invite_code;
        [moreview show];
    }
    
}
- (void)copyQRCode {
    
    [ByCoreToolAlert showOnlyText:@"复制成功"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSLog(@"url:%@", _model.url);
    pasteboard.string = _model.url;
}


@end
